import {_config, get_os, s94_log, unifyEnv} from "@/js/s94_tool.js";
import s94_remMjs from "@/js/s94_rem.mjs.js";
import s94Mjs from "s94-js";
import '@/assets/base.css'
import 'element-plus/dist/index.css'

import {createApp, watch} from 'vue'
import App from '@/App.vue'

import helpers from "@/vue_plugin/helpers.js";
import i18n, {LANG} from "@/vue_plugin/i18n/index.js";
import router_auto from "@/vue_plugin/router_auto.js";
import ElementPlus from 'element-plus'
import zh_cn from 'element-plus/dist/locale/zh-cn.mjs'
import en from 'element-plus/dist/locale/en.mjs'
import VMdPreview from '@kangc/v-md-editor/lib/preview';
import hljs from 'highlight.js';
import '@kangc/v-md-editor/lib/style/preview.css';
import githubTheme from '@kangc/v-md-editor/lib/theme/github.js';
import '@kangc/v-md-editor/lib/theme/style/github.css';
import list_url from "@/components/list_url.vue";
import forms from "@/components/forms.vue";
import menu_slot from "@/components/menu_slot.vue";
import list from "@/components/list.vue";
import {createPinia} from "pinia";
import pluginCache from "@/store/pluginCache.js";
import {use_config_map} from "@/store/config_map.js";
import {isDebug} from "@/js/debug_tool.js";

// ---------------------- 初始化 ----------------------
//版本
const VERSION = '1.2.1';
console.log(`VERSION=${VERSION}`);
//设备类型
s94Mjs.load.os = get_os();
//rem
s94_remMjs.setScale(0.8).display(600, 160);
//config初始化

// ---------------------- 页面构建 ----------------------
const vue = createApp(App)

const pinia = createPinia()
pinia.use(pluginCache);
vue.use(pinia)

// 助手函数
vue.use(helpers)

// 国际化
vue.use(i18n);

// 路由
vue.use(router_auto)

// element-plus
let locale_map = {
    zh_cn: zh_cn,
    en: en
}
const locale = locale_map[LANG] || {}
vue.use(ElementPlus, {locale})

// md文件解析
VMdPreview.use(githubTheme, {
    Hljs: hljs,
});
vue.use(VMdPreview)

// 全局组件
vue.component('list_url',list_url)
    .component('forms',forms)
    .component('menu_slot',menu_slot)
    .component('list',list);


let app = vue.mount('#app')

if (typeof window === 'object'){
    window['root_app'] = app;
}

export default app;


// 调试模式
if(isDebug()){
    // 错误事件绑定
    vue.config.errorHandler = function (err, vm, info){
        console.error(err, 'vue.config.errorHandler');
        s94_log(err.stack, info);
    }
    window.addEventListener('error', (evt)=>{
        console.error(evt.error || evt.message, 'window.onerror');
        if (typeof evt.error) s94_log(evt.error.stack, evt.error.message);
    })
}

//页面加载
if ("loading" == document.readyState) {
    document.addEventListener("DOMContentLoaded", function (){
        s94Mjs.load.init(s94Mjs.load.os)
    })
}else {
    s94Mjs.load.init(s94Mjs.load.os);
}

// 版本比较
// 判断版本，如果版本小于指定版本，执行 cache_load.init(true)
if (typeof cache_load === 'function' && typeof cache_load.init === 'function'){
    // let version_arr = s94Mjs.to_string(use_config_map().config.system?.vue_code_version).split('.');
    // if (VERSION.split('.').some((v,i)=>{ return parseInt(v) < parseInt(version_arr[i]); })) cache_load.init(true);
}
// 标题图标
watch(()=>use_config_map().this_tenant, (v, oldV)=>{
    console.log('this_tenant', v, oldV)
    s94Mjs(function(){
        //标题和图标
        document.title = v.title;
        document.querySelector('link[rel="icon"]').setAttribute('href', v.head_img)
    })
})
